/* *************************************** */
/* * Smooth - Sequential Edition         * */
/* * Authors:                            * */
/* *  André Bannwart Perina      7152757 * */
/* *  Rodrigo de Freitas Pereira 7573472 * */
/* *  Thaís Emanuele dos Santos  6453087 * */
/* *************************************** */

#include "../include/ppm_funcs.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

int main(void) {
	ppm_seg_t *r, *g, *b;
	ppm_seg_t *or, *og, *ob;
	struct timeval tEnd, tInit;
	struct timeval tPEnd, tPInit;

	gettimeofday(&tInit, NULL);

	/* Create segments */
	ppm_create(&r);
	ppm_create(&g);
	ppm_create(&b);
	ppm_create(&or);
	ppm_create(&og);
	ppm_create(&ob);

	/* Get channels from file */
	ppm_get_channel("in.ppm", PPM_CHANNEL_R, PPM_FORMAT_P3, r);
	ppm_get_channel("in.ppm", PPM_CHANNEL_G, PPM_FORMAT_P3, g);
	ppm_get_channel("in.ppm", PPM_CHANNEL_B, PPM_FORMAT_P3, b);

	gettimeofday(&tPInit, NULL);

	/* Add borders */
	ppm_add_border(r, PPM_BORDER_TOP, 2);
	ppm_add_border(r, PPM_BORDER_BOTTOM, 2);
	ppm_add_border(r, PPM_BORDER_LEFT, 2);
	ppm_add_border(r, PPM_BORDER_RIGHT, 2);
	ppm_add_border(g, PPM_BORDER_TOP, 2);
	ppm_add_border(g, PPM_BORDER_BOTTOM, 2);
	ppm_add_border(g, PPM_BORDER_LEFT, 2);
	ppm_add_border(g, PPM_BORDER_RIGHT, 2);
	ppm_add_border(b, PPM_BORDER_TOP, 2);
	ppm_add_border(b, PPM_BORDER_BOTTOM, 2);
	ppm_add_border(b, PPM_BORDER_LEFT, 2);
	ppm_add_border(b, PPM_BORDER_RIGHT, 2);

	/* Apply smooth */
	ppm_smooth(r, or);
	ppm_smooth(g, og);
	ppm_smooth(b, ob);

	/* Add borders */
	ppm_remove_border(or, PPM_BORDER_TOP, 2);
	ppm_remove_border(or, PPM_BORDER_BOTTOM, 2);
	ppm_remove_border(or, PPM_BORDER_LEFT, 2);
	ppm_remove_border(or, PPM_BORDER_RIGHT, 2);
	ppm_remove_border(og, PPM_BORDER_TOP, 2);
	ppm_remove_border(og, PPM_BORDER_BOTTOM, 2);
	ppm_remove_border(og, PPM_BORDER_LEFT, 2);
	ppm_remove_border(og, PPM_BORDER_RIGHT, 2);
	ppm_remove_border(ob, PPM_BORDER_TOP, 2);
	ppm_remove_border(ob, PPM_BORDER_BOTTOM, 2);
	ppm_remove_border(ob, PPM_BORDER_LEFT, 2);
	ppm_remove_border(ob, PPM_BORDER_RIGHT, 2);

	gettimeofday(&tPEnd, NULL);

	printf("Elapsed time (only processing): %lu us\n", (((tPEnd.tv_sec * 1000000) + tPEnd.tv_usec) - ((tPInit.tv_sec * 1000000) + tPInit.tv_usec)));

	/* Save file */
	ppm_set_file("out.ppm", PPM_FORMAT_P3, or, og, ob);

	/* Free stuff */
	ppm_destroy(&or);
	ppm_destroy(&og);
	ppm_destroy(&ob);
	ppm_destroy(&r);
	ppm_destroy(&g);
	ppm_destroy(&b);

	gettimeofday(&tEnd, NULL);

	printf("Total Elapsed time: %lu us\n", (((tEnd.tv_sec * 1000000) + tEnd.tv_usec) - ((tInit.tv_sec * 1000000) + tInit.tv_usec)));

	return EXIT_SUCCESS;
}